<?php
namespace app\backstage\logic;
use think\Db;

class Account
{
    /*单列数据获取*/
    public function getInfo($id)
    {
        $field = 'id,account,status';
        $where[] = ['id','=',$id];
        $Account = \think\facade\App::model('Account');

        return $Account->getOne($where, $field);
    }

    /*列表数据获取*/
    public function getAgentList($params)
    {
        $field = 'id,account,remarks,status,create_time,channel_code,sharing_ratio,scale';

        if(request()->_role->alias == 'eb')
        {
            $params['role_id'] = 5;
            $params['parent_id'] = request()->_user->id;
        }

        if(request()->_role->alias == 'agent')
        {
            $params['role_id'] = 9;
            $params['parent_id'] = request()->_user->id;
        }

        $condition[] = ['status', 'eq', 'egt:0','status'];
        $condition[] = ['account', 'like', 'require','account'];
        $condition[] = ['role_id', 'eq', 'require','role_id'];
        $condition[] = ['parent_id', 'eq', 'require','parent_id'];

        $where   = buildQueryCondition($condition, $params);

        if( isset($params['field']) ){
            $orderBy = [ $params['field'] => $params['order'] ];
        }else{
            $orderBy = ['id' => 'DESC'];
        }

        $Account = \think\facade\App::model('Account');
        $list = $Account->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if($list)
        {
            $state = ['0'=>'<span style="color: red">禁用</span>','1'=>'<span style="color: green">正常</span>'];

            foreach ($list as $k=>$v)
            {
                $quicks = Db::table('quick_app_mp_manager')->alias('a')->leftJoin('quick_app b','b.id=a.quick_id')
                    ->where('a.uid',$v['id'])->column('b.name');

                $list[$k]['state'] = $state[$v['status']];
                $list[$k]['quicks'] = $quicks ? implode('，',$quicks) : '';
            }
        }

        $result['count']    = $Account->getTotalNumber($where);
        $result['list']     = $list;
        return $result;
    }

    /*
     * 代理账号数据获取
     * $ebId int 商务ID
     * */
    public function getAgents($ebId = 0)
    {

        $role_ids = Db::table('cms_manager_role')->where('alias','=','agent')->column('id');

        if($ebId > 0)
        {
            $list = Db::table('cms_manager_user')->where('parent_id','=',$ebId)
                ->where('role_id','in',$role_ids)->where('status',1)
                ->field('id,account')->select();
        }else{

            $list = Db::table('cms_manager_user')->where('role_id','in',$role_ids)->where('status',1)
                ->field('id,account')->select();
        }


        return $list;
    }

    /*
     * 投放号数据获取
     * $agentId int 代理ID
     * */
    public function getChannels($agentId)
    {
        if($agentId > 0)
        {
            $list = Db::table('cms_manager_user')->where('parent_id','=',$agentId)->where('status',1)
                ->field('id,account')->select();
        }else{
            $role_ids = Db::table('cms_manager_role')->where('alias','=','channel')->column('id');
            $list = Db::table('cms_manager_user')->where('role_id','in',$role_ids)->where('status',1)
                ->field('id,account')->select();
        }

        return $list;
    }


    /*
     * 商务列表
     */
    public function getEbs(){
        $roleId = Db::table('cms_manager_role')->where('alias','=','eb')->value('id');
        $list = Db::table('cms_manager_user')->where('role_id','=',$roleId)->field('id,account')->select();
        return $list;
    }

}